﻿/*********************************************************
 * 项目：
 * 描述：
 * 作者：英夫美迪 冯东耀 simadi@qq.com
 * 日期：2013/1/8 9:16:55
 * *******************************************************/

using System.Collections.Generic;
using System.Configuration;
using Entity.TaoBao;

namespace Common
{
    public class TaoBao : HttpMethods
    {
        #region 字段
        /// <summary>
        /// App Key
        /// </summary>
        public string AppKey { get; set; }
        /// <summary>
        /// App Secret
        /// </summary>
        public string AppSecret { get; set; }

        public AccessToken AccessToken { get; set; }

        string CallBackUrl { get; set; }

        string RedirectUri { get; set; }
        string Token { get; set; }

        #endregion

        /// <summary>
        /// 构造函数
        /// </summary>
        public TaoBao(string accessToken="")
        {
            if (accessToken.Length==0)
            {
                var keyAndSecret = ConfigurationManager.AppSettings["KeySecretTB"]; //从配置文件中获取AppKey, AppSecret
                CallBackUrl = ConfigurationManager.AppSettings["CallBackUrlTB"];//从配置文件中获取回调地址
                RedirectUri = ConfigurationManager.AppSettings["redirect_uri"];//从配置文件中获取回调地址
                var arrayAppKeyAppSecret = keyAndSecret.Split(':');
                AppKey = arrayAppKeyAppSecret[0];
                AppSecret = arrayAppKeyAppSecret[1];
            }
            else
            {
                Token = accessToken;
            }
        }

        #region token相关

        public string GetCodeUrl()
        {
            var dictionary = new Dictionary<object, object>
                {
                    {"client_id",AppKey},
                    {"response_type","code"},
                    {"redirect_uri",RedirectUri}
                };
            //response_type=code&client_id={0}&redirect_uri={1}&state=1212&scope=item&view=web
            return string.Format("{0}?{1}", TbUrls.Authorize,dictionary.ToQueryString() );
        }
        /// <summary>
        /// 获取Request Token
        /// 默认 Http 方法为 GET
        /// </summary>
        /// <returns></returns>
        public AccessToken GetAccessToken(string code)
        {
            var dictionary = new Dictionary<object, object>
                {
                    {"client_id",AppKey},
                    {"client_secret",AppSecret},
                    {"grant_type","authorization_code"},
                    {"redirect_uri",RedirectUri},
                    {"code",code}
                };
            var url = string.Format("{0}?{1}", TbUrls.AccessToken, dictionary.ToQueryString());
            var jsonString = HttpPost(url);
            //Util.LogFile("qqToken" + result);
            return jsonString.ToEntity<AccessToken>();
        }
        #endregion

        string GetData(string method, string fields)
        {
            var dictionary = new Dictionary<object, object>
                {
                    {"method",method},
                    {"format","json"},
                    {"access_token",Token},
                    {"v","2.0"},
                    {"fields",fields}
                };
            var url = string.Format("{0}?{1}", TbUrls.ApiUrl, dictionary.ToQueryString());
            var jsonString = HttpPost(url);
            //Util.LogFile("qqToken" + result);
            return jsonString;
        }
        /// <summary>
        /// 查询卖家用户信息
        /// </summary>
        /// <param name="fields">默认返回全部字段</param>
        /// <returns></returns>
        public string UserSeller(string fields = "user_id,nick,sex,seller_credit,type,has_more_pic,item_img_num,item_img_size,prop_img_num,prop_img_size,auto_repost,promoted_type,status,alipay_bind,consumer_protection,avatar,liangpin,sign_food_seller_promise,has_shop,is_lightning_consignment,has_sub_stock,is_golden_seller,vip_info,magazine_subscribe,vertical_market,online_gaming")
        {
            return GetData("taobao.user.seller.get", fields);
        }
    }
}
